// SampleDialog.cpp : implementation file
//

#include "../stdafx.h"
#include "../Reflectometry.h"
#include "SampleDialog.h"
#include "afxdialogex.h"
#include "../3rdparty/CvvImage.h"

#include "../MainFrm.h"
#include "../ReflectometryDoc.h"
#include "../sky/ClearSky.h"
#include "../camera/SkyPhoto.h"


// SampleDialog dialog

IMPLEMENT_DYNAMIC(CSampleDialog, CDialogEx)

CSampleDialog::CSampleDialog(CWnd* pParent /*=NULL*/)
	: CDialogEx(CSampleDialog::IDD, pParent)
	, m_sampleX(0)
	, m_sampleY(0)
{
	m_curSkyPhotoIndex = -1;
//	m_sampleX = m_sampleY = 50;
}

CSampleDialog::~CSampleDialog()
{
}

void CSampleDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CSampleDialog, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_IMAGE, &CSampleDialog::OnBnClickedButtonLoadImage)
	ON_BN_CLICKED(IDC_BUTTON_AC, &CSampleDialog::OnBnClickedButtonAc)
	ON_BN_CLICKED(IDC_BUTTON_REJ, &CSampleDialog::OnBnClickedButtonRej)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CSampleDialog::OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_FITMODEL, &CSampleDialog::OnBnClickedButtonFitmodel)
END_MESSAGE_MAP()


// SampleDialog message handlers

void CSampleDialog::ShowImage(IplImage* pImage, UINT ID)
{
	CDC *pDC = GetDlgItem(ID)->GetDC();
	HDC hDC= pDC->GetSafeHdc();
	CRect rect;
	GetDlgItem(ID)->GetClientRect(&rect);
	CvvImage cimg;
	cimg.CopyOf(pImage);
	cimg.DrawToHDC(hDC, &rect);
	ReleaseDC(pDC);
}

void CSampleDialog::OnBnClickedButtonLoadImage()
{
	// TODO: Add your control notification handler code here
	CMainFrame* pMainFrame = (CMainFrame *)AfxGetMainWnd();
	CReflectometryDoc *pDoc = (CReflectometryDoc *)pMainFrame->GetActiveDocument();
	CClearSky *pSky = pDoc->GetClearSky();
	if (m_curSkyPhotoIndex < pSky->GetSkyPhotoCount() - 1)
		m_curSkyPhotoIndex++;
	else
	{
		//	message box
		MessageBox(_T("it is the last image!"), _T("WARNING"), MB_OK|MB_ICONWARNING);
		m_curSkyPhotoIndex = -1;
		return;
	}
	CSkyPhoto photo = pSky->GetSkyPhoto(m_curSkyPhotoIndex);
	ShowImage(photo.GetImage(), IDC_STATIC_SKYIMAGE);
	if (!photo.LoadSamplesFromFile())
		pSky->InitSampleFromSkyPhoto(m_curSkyPhotoIndex);

	GenerateSample();
}


void CSampleDialog::OnBnClickedButtonAc()
{
	// TODO: Add your control notification handler code here
	CMainFrame* pMainFrame = (CMainFrame *)AfxGetMainWnd();
	CReflectometryDoc *pDoc = (CReflectometryDoc *)pMainFrame->GetActiveDocument();
	CClearSky *pSky = pDoc->GetClearSky();
	pSky->SampleFromSkyPhoto(m_curSkyPhotoIndex, m_sampleX, m_sampleY, m_r, m_g, m_b);

	GenerateSample();
}

void CSampleDialog::GenerateSample()
{
	//	generate the first sample pixel
	CMainFrame* pMainFrame = (CMainFrame *)AfxGetMainWnd();
	CReflectometryDoc *pDoc = (CReflectometryDoc *)pMainFrame->GetActiveDocument();
	CClearSky *pSky = pDoc->GetClearSky();
	CSkyPhoto photo = pSky->GetSkyPhoto(m_curSkyPhotoIndex);
	int width, height;
	width = photo.GetWidth();
	height = photo.GetHeight();

	
	m_sampleX = (int)(width * m_rng());
	m_sampleY = (int)(height * m_rng());
	photo.Pixel2Color(m_sampleX, m_sampleY, m_r, m_g, m_b);
	CSkyPhoto dupPhoto(photo);
	for (int i = 0; i < width; i++)
		dupPhoto.SetPixelColor(i, m_sampleY, 255, 0, 0);
	for (int i = 0; i < height; i++)
		dupPhoto.SetPixelColor(m_sampleX, i, 255, 0, 0);
	//photo.SetPixelColor(m_sampleX, m_sampleY, 255, 0, 0);
	ShowImage(dupPhoto.GetImage(), IDC_STATIC_SKYIMAGE);
}

void CSampleDialog::OnBnClickedButtonRej()
{
	// TODO: Add your control notification handler code here
	GenerateSample();
}



void CSampleDialog::OnBnClickedButtonSave()
{
	// TODO: Add your control notification handler code here
	CMainFrame* pMainFrame = (CMainFrame *)AfxGetMainWnd();
	CReflectometryDoc *pDoc = (CReflectometryDoc *)pMainFrame->GetActiveDocument();
	CClearSky *pSky = pDoc->GetClearSky();
	int count = pSky->GetSkyPhotoCount();
	for (int i = 0; i < count; i++)
	{
		CSkyPhoto photo = pSky->GetSkyPhoto(m_curSkyPhotoIndex);
		photo.SaveSamplesIntoFile();
	}
}


void CSampleDialog::OnBnClickedButtonFitmodel()
{
	// TODO: Add your control notification handler code here
	CMainFrame* pMainFrame = (CMainFrame *)AfxGetMainWnd();
	CReflectometryDoc *pDoc = (CReflectometryDoc *)pMainFrame->GetActiveDocument();
	CClearSky *pSky = pDoc->GetClearSky();
	pSky->SkyModelFitting(m_curSkyPhotoIndex);
	
	CSkyPhoto photo = pSky->GetSkyPhoto(m_curSkyPhotoIndex);

	int width = photo.GetWidth();
	int height = photo.GetHeight();
	CSkyPhoto dupPhoto(photo);
	int r, g, b;
	for (int i = 0; i < width; i++)
		for (int j = 0; j < height; j++)
		{
			photo.Pixel2SkyModelColor(i, j, r, g, b);
			dupPhoto.SetPixelColor(i, j, r, g, b);
		}
	dupPhoto.SaveSkyPhoto();
	//photo.SetPixelColor(m_sampleX, m_sampleY, 255, 0, 0);
//	ShowImage(dupPhoto.GetImage(), IDC_STATIC_SKYIMAGE);

}
